﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "UNC" here, you must also update the reference to "UNC" in App.config.
    public partial class MainService : IUNC
    {
        public int UNC_Using(int UNCID)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_UNC_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@UNC_ID", UNCID);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool UNC_CheckExist(int _ID, string BranchCode, DateTime _Date, string _No)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_UNC_CheckExist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ID", _ID);
            dbCmd.Parameters.AddWithValue("@BranchCode", BranchCode);
            dbCmd.Parameters.AddWithValue("@Date", _Date);
            dbCmd.Parameters.AddWithValue("@No", _No);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)
                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public decimal UNC_GetAmountPaid(int _Month, int _Year, string BranchCode)
        {
            decimal retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_UNC_GetAmountPaid", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@BranchCode", BranchCode);
            dbCmd.Parameters.AddWithValue("@Month", _Month);
            dbCmd.Parameters.AddWithValue("@Year", _Year);
            try
            {
                dbConn.Open();
                retVal = Convert.ToDecimal(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet UNC_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_UNC_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet UNC_GetList(out int ItemCount, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_UNC_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public UNCInfo UNC_GetInfo(int UNCID)
        {
            UNCInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_UNC_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@UNC_ID", UNCID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new UNCInfo();
                    retVal.ID = Convert.ToInt32(dr["UNC_ID"]);
                    retVal.No = dr["UNC_No"].ToString();
                    retVal.Date = dr["UNC_Date"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["UNC_Date"]);
                    retVal.Year = Convert.ToInt32(dr["UNC_Year"]);
                    retVal.Month = Convert.ToInt32(dr["UNC_Month"]);
                    retVal.Quater = dr["UNC_Quater"].ToString();
                    retVal.Content = dr["UNC_Content"].ToString();
                    retVal.BranchCode = dr["UNC_BranchCode"].ToString();
                    retVal.Amount = Convert.ToDecimal(dr["UNC_Amount"]);                
                    retVal.Discription = dr["UNC_Description"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int UNC_Insert(UNCInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_UNC_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@UNC_No", eInfo.No);
            dbCmd.Parameters.AddWithValue("@UNC_Quater", eInfo.Quater);
            dbCmd.Parameters.AddWithValue("@UNC_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@UNC_Month", eInfo.Month);
            dbCmd.Parameters.AddWithValue("@UNC_BranchCode", eInfo.BranchCode);
            dbCmd.Parameters.AddWithValue("@UNC_Content", eInfo.Content);
            dbCmd.Parameters.AddWithValue("@UNC_Year", eInfo.Year);
            dbCmd.Parameters.AddWithValue("@UNC_Amount", eInfo.Amount);
            dbCmd.Parameters.AddWithValue("@UNC_Description", eInfo.Discription);

            try
            {

                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool UNC_Update(UNCInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_UNC_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@UNC_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@UNC_No", eInfo.No);
            dbCmd.Parameters.AddWithValue("@UNC_Quater", eInfo.Quater);
            dbCmd.Parameters.AddWithValue("@UNC_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@UNC_Month", eInfo.Month);
            dbCmd.Parameters.AddWithValue("@UNC_BranchCode", eInfo.BranchCode);
            dbCmd.Parameters.AddWithValue("@UNC_Content", eInfo.Content);
            dbCmd.Parameters.AddWithValue("@UNC_Year", eInfo.Year);
            dbCmd.Parameters.AddWithValue("@UNC_Amount", eInfo.Amount);
            dbCmd.Parameters.AddWithValue("@UNC_Description", eInfo.Discription);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool UNC_Delete(int UNCID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_UNC_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@UNC_ID", UNCID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
